use "dta\DataSet_v28.dta" , clear

************************************************************************
************************* SAMPLE RESTRICTION ***************************
************************************************************************
keep if inlist(party, "sv", "dna", "sp", "v", "krf", "h", "frp", "h_fv")
keep if year>1920

************************************************************************
************************* DEFINE SENIORITY *****************************
************************************************************************
sort pid year
gen run_prev=0
forvalues i=1(1)15{
	replace run_prev=`i' if pid == pid[_n-`i']
	}

gen seniority=run_prev
gen seniorityXfemale=seniority*female

li cand*ed year seniority if candidatename_ed=="Trygve Martin Bratteli"
li cand*ed year seniority if candidatename_ed=="Erna Solberg"

************************************************************************
************************* RANK VARIABLES *******************************
************************************************************************
drop rank1 rank2 /* variables referring to the 1906-1918 electoral system*/

* Generate rank variables
forval i = 1/5 {
    gen rank`i' = (rank == `i')
}

* Generate interaction terms between rank variables and female
forval i = 1/5 {
    gen rank`i'_female = rank`i' * female
}

************************************************************************
************************* INCLUDE MP EDUCATION *************************
************************************************************************
merge m:1 year pid using "dta\MP_education"

gen high_educationXfemale=high_education*female


************************************************************************
************************* SAMPLE RESTRICTION ***************************
************************************************************************
egen partycabinet=sum(cabinet), by(year party)
keep if year>1944 & partycabinet>0


************************************************************************
************************* REGRESSION ***********************************
************************************************************************

egen party_year=group(party year)

*keep if year>1980

************************************************************************
************************* REGRESSION ***********************************
************************************************************************

eststo clear
eststo: reg cabinet female seniority rank1, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 seniorityXfemale rank1_female i.party_year, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 if elected==1, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 seniorityXfemale rank1_female i.party_year if elected==1, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 seniorityXfemale rank1_female high_education high_educationXfemale i.party_year if elected==1, cluster(pid)
qui estadd ysumm
esttab using tables\Table1.tex, replace style(tex) se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) drop(*ons* *party_year*) mtitles("All" "All" "MPs" "MPs" "MPs") label ///
coeflabels(rank1 "List Position 1" rank2 "List Position 2" rank3 "List Position 3" rank4 "List Position 4" rank5 "List Position 5" ///
rank1_female "List Position 1 X Female" rank2_female "List Position 2 X Female" rank3_female "List Position 3 X Female" rank4_female "List Position 4 X Female" rank5_female "List Position 5 X Female" ///
seniority "Seniority" seniorityXfemale "Seniority X Female" female "Female" high_education "High Education" high_educationXfemale "High Education X Female") ///
nonotes stats(ymean r2 N, labels("Mean of outcome var." "R-squared" "Observations") fmt(3 2 0)) 

************************************************************************
************************* REGRESSION ***********************************
************************************************************************

eststo clear
eststo: reg cabinet female seniority rank1 rank2 rank3 rank4 rank5 i.party_year, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 rank2 rank3 rank4 rank5 seniorityXfemale rank1_female rank2_female rank3_female rank4_female rank5_female i.party_year, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 rank2 rank3 rank4 rank5 i.party_year if elected==1, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 rank2 rank3 rank4 rank5 seniorityXfemale rank1_female rank2_female rank3_female rank4_female rank5_female i.party_year if elected==1, cluster(pid)
qui estadd ysumm
eststo: reg cabinet female seniority rank1 rank2 rank3 rank4 rank5 seniorityXfemale rank1_female rank2_female rank3_female rank4_female rank5_female high_education high_educationXfemale i.party_year if elected==1, cluster(pid)
qui estadd ysumm
esttab using tables\TableA3.tex, replace style(tex) se b(%9.3f) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) drop(*ons* *party_year*) mtitles("All" "All" "MPs" "MPs" "MPs") label ///
coeflabels(rank1 "List Position 1" rank2 "List Position 2" rank3 "List Position 3" rank4 "List Position 4" rank5 "List Position 5" ///
rank1_female "List Position 1 X Female" rank2_female "List Position 2 X Female" rank3_female "List Position 3 X Female" rank4_female "List Position 4 X Female" rank5_female "List Position 5 X Female" ///
seniority "Seniority" seniorityXfemale "Seniority X Female" female "Female" high_education "High Education" high_educationXfemale "High Education X Female") ///
nonotes stats(ymean r2 N, labels("Mean of outcome var." "R-squared" "Observations") fmt(3 2 0)) 
